<script setup>
import imgPath from '@/assets/house-img.jpg'
import {useRouter} from 'vue-router';
import {getImagesApi} from '../api/house/house.js'

// 定义props
const props = defineProps({
  house: {
    type: Object,
    default: () => {
      return {
        id: 1,
        title: '汤臣一品',
        address: '深圳市南山区留仙大道深铁阅山境花园',
        rent: 5000,
        area: 100,
        floor: 5,
      }
    }
  },
})

// 使用 useRoute 获取路由对象
const router = useRouter();
// 图片
const img = ref("")

const toDetail = () => {
  router.push({
    path: '/house-detail/' + props.house.id
  })
}

onMounted(() => {
  getImagesApi(props.house.id).then(res => {
    const imgs = res.data.split(",")
    img.value = imgs.length > 0 && imgs[0] ? `/dev-api/${imgs[0]}` : imgPath
  })
})
</script>

<template>
  <div class="house-card-item" @click="toDetail">
    <img :src="img">
    <h1 class="title">
      {{ house.houseName }}
      <el-tag :type="house.status == '0'? 'success' : 'default'">
        {{ house.status == '0' ? "未租出" : "已出租" }}
      </el-tag>
    </h1>
    <p class="address"> {{ house.description }}</p>
    <div class="price">
      <span class="price-num">￥ {{ house.rent }}</span>
      <span class="price-unit">/月</span>
    </div>
    <div class="info">
      <p class="info-item">面积：{{ house.area }}m²</p>
      <p class="info-item">楼层：{{ house.floor }}F</p>
    </div>
  </div>
</template>

<style scoped>
.house-card-item {
  margin-bottom: 30px;
  padding-bottom: 20px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;

  &:hover {
    transform: scale(1.05);
  }

  img {
    width: 100%;
    height: 240px;
    object-fit: cover;
  }

  .title {
    font-size: 24px;
    font-weight: bold;
    color: #000;
    margin: 20px 0;
    padding: 0 20px;
  }

  .address {
    font-size: 16px;
    color: #999;
    margin-bottom: 20px;
    padding: 0 20px;
  }

  .price {
    font-size: 24px;
    color: #4abb97;
    margin-bottom: 20px;
    padding: 0 20px;
  }

  .info {
    font-size: 16px;
    color: #999;
    padding: 0 20px;
  }
}
</style>
